package com.cmcc.linlaker.ui.upload;

import android.app.DatePickerDialog;
import android.graphics.Paint;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.DatePicker;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.cmcc.linlaker.R;
import com.cmcc.linlaker.http.ApiDefine;
import com.cmcc.linlaker.ui.mine.bean.CheckRankingBean;
import com.cmcc.linlaker.ui.upload.adapter.PlanAdapter;
import com.cmcc.linlaker.ui.upload.bean.PlanBean;
import com.cmcc.linlaker.utils.BarUtils;
import com.cmcc.linlaker.utils.BasisTimesUtils;
import com.cmcc.linlaker.utils.IntentUtils;
import com.cmcc.linlaker.utils.SPHelper;
import com.cmcc.linlaker.volley.GsonRequestHelper;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CheckPlanDetailActivity extends AppCompatActivity {
    private String date;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_check_detail);

        BarUtils.initStatusBar(this);

        date = getIntent().getStringExtra("date");

        initView();
    }

    private TextView text_date;
    private Calendar calendar;

    private PlanAdapter adapter;
    private RecyclerView recycler_plan;
    private List<PlanBean.PlanData> itemList = new ArrayList<>();

    private void initView() {
        calendar = Calendar.getInstance();
        text_date = findViewById(R.id.text_date);

        text_date.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG); //下划线
        text_date.getPaint().setAntiAlias(true);//抗锯齿

        recycler_plan = findViewById(R.id.recycler_plan);
        setRecyclerView(recycler_plan, itemList);

        text_date.setText(date);
        getTaskList(date);
    }

    private void setRecyclerView(RecyclerView recycler_view, final List<PlanBean.PlanData> itemList) {
        recycler_view.setHasFixedSize(true);
        recycler_view.setLayoutManager(new LinearLayoutManager(this));

        recycler_view.setNestedScrollingEnabled(false);

        adapter = new PlanAdapter(this, itemList);
        recycler_view.setAdapter(adapter);
    }

    public void back(View view) {
        finish();
    }

    /**
     * 选择月份
     */
    public void selectMonth(View view) {
        showYearMonthPicker();
    }

    /**
     * 年月选择
     */
    private void showYearMonthPicker() {
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        new DatePickerDialog(CheckPlanDetailActivity.this, myDateSetListene, year, month, day).show();
    }

    /**
     * 数字小于10 前面加0
     */
    private String parseNumber(int number) {
        if (number < 10) {
            return "0" + number;
        } else {
            return number + "";
        }
    }

    /**
     * 量化考核排行榜
     */
    private void getTaskList(String date) {
        Log.e("date", date);

        GsonRequestHelper helper = new GsonRequestHelper(this);
        Map<String, String> params = new HashMap<>();
        params.put("planTime", date);
        params.put("userId", SPHelper.getUid());

        helper.sendPOSTRequest(ApiDefine.TASK_LIST_GROUP, PlanBean.class, params, false, null);
        helper.setOnResponseListener(new GsonRequestHelper.OnResponseListener() {
            @Override
            public void onError(VolleyError error) {

            }

            @Override
            public void onSuccess(Object data) {
                if (data != null) {
                    PlanBean bean = (PlanBean) data;
                    if (bean.code.equals("0")) {
                        itemList.clear();
                        itemList.addAll(bean.rows);
                        adapter.notifyDataSetChanged();

                        if (itemList.size() > 0) {
                            recycler_plan.setVisibility(View.VISIBLE);
                            findViewById(R.id.layout_no_data).setVisibility(View.GONE);
                        } else {
                            recycler_plan.setVisibility(View.GONE);
                            findViewById(R.id.layout_no_data).setVisibility(View.VISIBLE);
                        }
                    } else if (bean.code.equals(IntentUtils.VAILD_CODE)) {
                        IntentUtils.skipToLogin(CheckPlanDetailActivity.this);
                    }
                }
            }
        });
    }

    /**
     *  * 日期选择器对话框监听
     *  
     */
    private DatePickerDialog.OnDateSetListener myDateSetListene = new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
            String dateStr = year + "-" + parseNumber(month) + "-" + parseNumber(dayOfMonth);
            text_date.setText(dateStr);
            getTaskList(dateStr);
        }
    };
}
